# A valid snippet should starts with:
#
#		snippet trigger_word [ "description" [ options ] ]
#
# and end with:
#
#		endsnippet
#
# Snippet options:
#
#		b - Beginning of line.
#		i - In-word expansion.
#		w - Word boundary.
#		r - Regular expression
#		e - Custom context snippet
#		A - Snippet will be triggered automatically, when condition matches.
#
# Basic example:
#
#		snippet emitter "emitter properties" b
#		private readonly ${1} = new Emitter<$2>()
#		public readonly ${1/^_(.*)/$1/}: Event<$2> = this.$1.event
#		endsnippet
#
# Online reference: https://github.com/SirVer/ultisnips/blob/master/doc/UltiSnips.txt

snippet cmake_minimum "minimum CMakeLists.txt" b
cmake_minimum_required (VERSION 3.10)
set(projectName ${1:demo})
project (\${projectName})

#include(auto_version.cmake)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_C_COMPILER gcc)
set(CMAKE_CXX_COMPILER g++)
set(CMAKE_CXX_STANDARD 11)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY \${CMAKE_SOURCE_DIR}/bin) 
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY \${CMAKE_SOURCE_DIR}/bin) 
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY \${CMAKE_SOURCE_DIR}/bin) 
# set(EXECUTABLE_OUTPUT_PATH \${CMAKE_SOURCE_DIR}/bin)
# set(LIBRARY_OUTPUT_PATH    \${CMAKE_SOURCE_DIR}/bin)

# include_directories (\${CMAKE_SOURCE_DIR}/add/src)

# link_directories (\${CMAKE_SOURCE_DIR}/build/archive)

add_executable(
	\${projectName}
	main.c
	# src1.c
	)

target_link_libraries (
	\${projectName} 
	libgtest.a
	)
endsnippet


snippet cmake_version "auto generate file of including version" b
set (${1:demo}_VERSION_MAJOR 1)
set ($1_VERSION_MINOR 0)

configure_file (
  "${PROJECT_SOURCE_DIR}/$1Config.h.in"
  "${PROJECT_BINARY_DIR}/$1Config.h"
	)
# 在$1Config.h.in中添加如下内容
##define $1_VERSION_MAJOR @$1_VERSION_MAJOR@
##define $1_VERSION_MINOR @$1_VERSION_MINOR@
# 
include_directories(${PROJECT_BINARY_DIR})
endsnippet


snippet cmake_library "generate shared library and static library" b
# 顶层cmake里使用add_subdirectory时，
# link_directories里不需要指定目录
# target_link_libraries里只需要指定库的名称, 不需要添加前缀lib以及文件扩展名

# 本级cmake里使用target_include_directories 
# 顶层cmake里不需要include_directories里指定目录
# 也可以在顶层中使用target_include_directories代替include_directories
aux_source_directory (. SRC_LIST)

add_library (${1:name}_shared SHARED \${SRC_LIST})
target_include_directories($1_shared PUBLIC ${PROJECT_SOURCE_DIR})

add_library ($1_static STATIC \${SRC_LIST})
target_include_directories($1_static PUBLIC ${PROJECT_SOURCE_DIR})

# 如果上层cmake设置了这个变量，已上层的为准
set (LIBRARY_OUTPUT_PATH \${PROJECT_SOURCE_DIR}/lib)
endsnippet


snippet cmake_message "print some variable" b
message("---------------------------------------------------------")
message("01.PROJECT_NAME                   = \${PROJECT_NAME}")
message("02.CMAKE_CURRENT_LIST_FILE        = \${CMAKE_CURRENT_LIST_FILE}")
message("")

message("a1.CMAKE_SOURCE_DIR               = \${CMAKE_SOURCE_DIR}") # 最外层CMakeLists.txt所在目录
message("a2.PROJECT_SOURCE_DIR             = \${PROJECT_SOURCE_DIR}")
message("a3.CMAKE_CURRENT_SOURCE_DIR       = \${CMAKE_CURRENT_SOURCE_DIR}")  # 当前CMakeLists.txt所在目录
message("")

message("b1.CMAKE_BINARY_DIR               = \${CMAKE_BINARY_DIR}")  # 运行cmake命令的目录
message("b2.PROJECT_BINARY_DIR             = \${PROJECT_BINARY_DIR}")
message("b3.CMAKE_CURRRENT_BINARY_DIR      = \${CMAKE_CURRRENT_BINARY_DIR}")
message("")

message("c1.CMAKE_RUNTIME_OUTPUT_DIRECTORY = \${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
message("c2.CMAKE_ARCHIVE_OUTPUT_DIRECTORY = \${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}")
message("c3.CMAKE_LIBRARY_OUTPUT_DIRECTORY = \${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
message("c4.EXECUTABLE_OUTPUT_PATH         = \${EXECUTABLE_OUTPUT_PATH}")
message("c5.LIBRARY_OUTPUT_PATH            = \${LIBRARY_OUTPUT_PATH}")
message("")

message("18.BUILD_SHARED_LIBS              = \${BUILD_SHARED_LIBS}")
message("---------------------------------------------------------")
endsnippet

snippet cmake_qt_project "cmake for qt widget/console project" b
cmake_minimum_required(VERSION 3.10)

project (${1:project_name} LANGUAGES CXX)

set (CMAKE_C_COMPILER "gcc")
set (CMAKE_CXX_COMPILER g++)

set (CMAKE_INCLUDE_CURRENT_DIR ON)
set (CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_STANDARD_REQUIRED ON)

set (CMAKE_AUTOUIC_SEARCH_PATHS ${CMAKE_SOURCE_DIR}/ui)
set (CMAKE_AUTOUIC ON)
set (CMAKE_AUTOMOC ON)
set (CMAKE_AUTORCC ON)

SET (EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

find_package (Qt5 COMPONENTS Core REQUIRED)
find_package (Qt5 COMPONENTS Gui REQUIRED)
find_package (Qt5 COMPONENTS Widgets REQUIRED)
find_package (Qt5 COMPONENTS Network REQUIRED)
find_package (Qt5 COMPONENTS PrintSupport REQUIRED)

# set(APP_ICON_RESOURCE_WINDOWS ${CMAKE_SOURCE_DIR}/qss/app.rc)		# 设置exe图标

set(SOURCES 
    ${CMAKE_SOURCE_DIR}/main.cpp
    ${CMAKE_SOURCE_DIR}/mainwindow.cpp
	)

add_executable(
	$1
    ${SOURCES}
    # ${CMAKE_SOURCE_DIR}/res.qrc
	# resources.qrc
)

target_link_libraries(
	$1 PRIVATE 
    Qt5::Core 
    Qt5::Gui
    Qt5::Widgets 
    Qt5::Network 
    Qt5::PrintSupport
)
endsnippet

snippet cmake_libuv "cmake for libuv" !b
cmake_minimum_required (VERSION 3.10)
set(projectName libuv_study)
project (${projectName})

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) # 静态库位置
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) # 动态库位置
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) # 可执行文件位置

add_subdirectory(libuv)
include_directories (libuv/include)

add_executable(
    ${projectName}
    main.c
)

target_link_libraries (
    ${projectName} 
    uv
)
endsnippet

snippet if "" !b
if (${1:UNIX})
	$2
ENDIF()
endsnippet

snippet cmake_git_commit "get git commit hash" 
execute_process(
  COMMAND git rev-parse --short=7 HEAD
  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  OUTPUT_VARIABLE GIT_COMMIT_HASH
  OUTPUT_STRIP_TRAILING_WHITESPACE)
endsnippet

snippet cmake_git_time "get git commit time"
execute_process(
  COMMAND git log -1 --format=%ct
  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  OUTPUT_VARIABLE GIT_COMMIT_TIMESTAMP
  OUTPUT_STRIP_TRAILING_WHITESPACE)

# 将时间戳转换为可读格式
execute_process(
  COMMAND date -d @${GIT_COMMIT_TIMESTAMP} "+%Y-%m-%d"
  OUTPUT_VARIABLE GIT_COMMIT_TIME
  OUTPUT_STRIP_TRAILING_WHITESPACE)
endsnippet

